﻿using System;
using System.Collections.Generic;
using System.Text;

namespace dnsServerCSharp
{
    public class Filter
    {
        public void remove(string name)
        {
            names.Remove(name);
        }
        public void insert(string name)
        {
            name.Replace('@', '.');
            if (!names.ContainsKey(name))
            {
                names.Add(name, true);
            }
        }
        public bool has(string name)
        {
            List<string> ns = new List<string>();
            string curname = "";
            var substrings = name.Split(DnsMessage.separator);
            Array.Reverse(substrings);
            foreach (string s in substrings)
            {
                curname = s + curname;
                ns.Add(curname);
                curname = '.' + curname;
            }
            foreach(string n in ns)
            {
                if (names.ContainsKey(n))
                    return true;
            }
            return false;
        }

        public long size 
        {
            get
            {
                return names.Count;
            }
        }
        Dictionary<string, bool> names = new Dictionary<string, bool>();
    }
}
